package de.herberlin.pss.ui;

import java.util.LinkedList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import de.herberlin.pss.action.GetSonglistAction;
import de.herberlin.pss.model.Artist;
import de.herberlin.pss.model.Song;

public class SongTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;

	public static final String[] COLS = { Song.Key.cSongTitle.name(),
			Artist.Key.cArtistName.name() };

	private SongTableModel() {
		// updateContent();
	}

	private List<Song> songList = new LinkedList<Song>();

	public void updateContent() {
		new GetSonglistAction(this).start();
	}

	@Override
	public String getColumnName(int pos) {
		return Text.getText(COLS[pos]);
	}

	public int getColumnCount() {
		return COLS.length;
	}

	public int getRowCount() {
		return songList.size();
	}

	public Object getValueAt(int row, int col) {
		Song song = songList.get(row);
		switch (col) {
		case 0:
			return song.get(Song.Key.cSongTitle);
		case 1:
			if (song.getArtist() != null) {
				return song.getArtist().get(Artist.Key.cArtistName);
			} else {
				return null;
			}
		}
		return null;
	}

	public Song getSongAt(int row) {
		if (songList == null) {
			return null;
		} else if (row > songList.size()) {
			return null;
		} else {
			return songList.get(row);
		}
	}

	public void setSonglist(List<Song> songList) {
		this.songList=songList;
		fireTableDataChanged();
	}
	private static SongTableModel _instance = null;

	public static SongTableModel getInstance() {
		if (_instance == null) {
			_instance = new SongTableModel();
		}
		return _instance;
	}

}
